import {
  BrowserRouter as Router,
  Link,
  Route,
  Switch,
  Redirect,
  useHistory,
} from "react-router-dom";
import { lazy, Suspense } from "react";
const Home = lazy(() => import("../pages/Home"));
const Login = lazy(() => import("../pages/Login"));

function ChildRouter() {
  const token = localStorage.getItem("token");
  const history = useHistory();
  if (!token) {
    history.push("/login");
  } else {
    history.push("/home");
  }
  return (
    <>
      <Suspense fallback={<div>Loading...</div>}>
        <Route path="/home" component={Home} />
      </Suspense>
      <Suspense fallback={<div>Loading...</div>}>
        <Route path="/login" component={Login} />
      </Suspense>
    </>
  );
}

const RouterConfig = () => {
  return (
    <Router>
      <ChildRouter />
    </Router>
  );
};

export default RouterConfig;
